package com.wrial.kind.doublepointer;
/*
 * @Author  Wrial
 * @Date Created in 14:36 2020/8/7
 * @Description
 */

public class ValidPalindromeII {

    /**
     * 双指针前后匹配，如果不匹配可以尝试删除左或者右其中一个继续尝试
     * @param s
     * @return
     */
    public boolean validPalindrome(String s) {
        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) == s.charAt(right)) {
                left++;
                right--;
            } else {
                return adjust(s, left, right - 1) || adjust(s, left + 1, right);
            }
        }
        return true;
    }

    private boolean adjust(String s, int left, int right) {
        while (left < right) {
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}
